﻿using System;
using System.Collections.Generic;
using System.Text;
using KSWSClient.Code.IService;
using System.Net;
using System.IO;
using System.Web;
using System.Xml;

namespace KSWSClient.Code.Service
{
    public class SMService:ISMService
    {
        public bool ValidateURL(string url)
        {
            bool result = true;
            try
            {
                WebClient wc = new WebClient();
                Stream str = wc.OpenRead(url);
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public String SendData(string url,string data)
        {
            string resultStr = String.Empty;
            // 创建Http请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "text/xml";
            // URL编码数据
            byte[] requestData = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(data.Trim()));
            request.ContentLength = requestData.Length;
            // 获取请求流
            Stream writer = request.GetRequestStream();
            writer.Write(requestData, 0, requestData.Length);
            writer.Close();
            // 响应数据
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            resultStr = reader.ReadToEnd();
            return resultStr;
        }
    }
}
